package com.wxm.common.file.start.exception;

import com.wxm.common.file.core.exception.FileException;
import com.wxm.common.file.start.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *      全局异常处理
 * </p>
 *
 * @author: wxm
 * @Date: 2021/9/20
 */
@Slf4j
@ResponseBody
@ControllerAdvice
public class ExceptionDispose {

    @ExceptionHandler(FileException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R<String> baseException(HttpServletRequest request, FileException e){
        return this.error(request,e,e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R<String> handleException(HttpServletRequest request, Exception e){
        return this.error(request,e,e.getMessage());//系统异常
    }

    private R<String> error(HttpServletRequest request,Exception e ,String message){
        String method = request.getMethod();
        e.printStackTrace();
        String requestUrl = request.getRequestURI();
        log.error("method:"+method+" requestUrl:"+requestUrl);
        return R.error(e.getMessage());
    }
}
